//
//  XMApplyForReturnView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/20.
//

#import "XMApplyForReturnView.h"
#import "XMConfirmOrderMeetCell.h"

@interface XMApplyForReturnView ()<UITableViewDelegate, UITableViewDataSource, UITextViewDelegate>

@property (nonatomic, strong) UITableView *tbl;

@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) XMConfirmOrderMeetCell *meetInfoView; //见面包信息

@property (nonatomic, strong) UILabel *payPriceLabel;    //实付金额
@property (nonatomic, strong) UILabel *returnPriceLabel;  //退款金额

@property (nonatomic, strong) UIView *ruleBgView; //退款规则

@property (nonatomic, strong) UIView *returnTypeBgView;  //退款方式

@property (nonatomic, strong) UITextView *explainTextView;  //说明
@property (nonatomic, strong) UILabel *placeHolderLabel;
@property (nonatomic, strong) UILabel *wordNumLabel;

@property (nonatomic, assign) NSInteger myReasonSelectIndex;
@property (nonatomic, assign) NSInteger otherReasionSelectIndex;

@end

@implementation XMApplyForReturnView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"申请退款";
    
    [self addSubview:self.tbl];
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(400.0))];
    headView.backgroundColor = kColorHEX(0xF0F0F0);
    //见面详情
    UIView *meetInfoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(162.0))];
    meetInfoBgView.backgroundColor = kColorHEX(0xffffff);
    [headView addSubview:meetInfoBgView];
    {
        [meetInfoBgView addSubview:self.avatarImgView];
        [meetInfoBgView addSubview:self.nameLabel];
        UIView *infoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(58.0), self.width, KScaleWidth(100.0))];
        [infoBgView addSubview:self.meetInfoView];
        [meetInfoBgView addSubview:infoBgView];
    }
    //价格界面
    UIView *priceInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, meetInfoBgView.maxY + KScaleWidth(8.0), self.width, KScaleWidth(183.0))];
    priceInfoView.backgroundColor = kColorHEX(0xffffff);
    [headView addSubview:priceInfoView];
    {
        UIView *pointView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(7.0) + (KScaleWidth(32.0) - KScaleWidth(6.0))/2, KScaleWidth(6.0), KScaleWidth(6.0))];
        pointView.backgroundColor = kColorHEX(0xE2E2E2);
        [pointView setCornerRadius:pointView.height/2];
        [priceInfoView addSubview:pointView];
        
        UILabel *payTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), KScaleWidth(7.0), self.width - KScaleWidth(28.0), KScaleWidth(32.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
        payTitleLabel.text = @"实付金额";
        [priceInfoView addSubview:payTitleLabel];
        
        UIView *pointView2 = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), payTitleLabel.maxY + (KScaleWidth(32.0) - KScaleWidth(6.0))/2, KScaleWidth(6.0), KScaleWidth(6.0))];
        pointView2.backgroundColor = kColorHEX(0xE2E2E2);
        [pointView2 setCornerRadius:pointView2.height/2];
        [priceInfoView addSubview:pointView2];
        
        UILabel *returnTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(payTitleLabel.x, payTitleLabel.maxY, payTitleLabel.width, payTitleLabel.height) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
        returnTitleLabel.text = @"可退金额";
        [priceInfoView addSubview:returnTitleLabel];
        [priceInfoView addSubview:self.payPriceLabel];
        [priceInfoView addSubview:self.returnPriceLabel];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, returnTitleLabel.maxY + KScaleWidth(3.0), self.width, 1)];
        lineView.backgroundColor = kColorHEX(0xF1F1F1);
        [priceInfoView addSubview:lineView];
        UILabel *ruleTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), lineView.maxY + KScaleWidth(3.0), self.width - KScaleWidth(16.0), KScaleWidth(32.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
        ruleTitleLabel.text = @"退款规则";
        [priceInfoView addSubview:self.ruleBgView];
        [priceInfoView addSubview:ruleTitleLabel];
    }
    //退款信息
    UIView *returnTypeBgView = [[UIView alloc] initWithFrame:CGRectMake(0, priceInfoView.maxY + KScaleWidth(8.0), self.width, KScaleWidth(82.0))];
    returnTypeBgView.backgroundColor = kColorHEX(0xffffff);
    _returnTypeBgView = returnTypeBgView;
    [headView addSubview:returnTypeBgView];
    {
        UILabel *typeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(12.0), self.width - KScaleWidth(16.0)*2, KScaleWidth(30.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
        typeTitleLabel.text = @"退还方式";
        [returnTypeBgView addSubview:typeTitleLabel];
        UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), typeTitleLabel.maxY, KScaleWidth(70.0), KScaleWidth(30.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
        typeLabel.text = @"原路退回";
        [returnTypeBgView addSubview:typeLabel];
        UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(120.0), typeLabel.y, self.width - KScaleWidth(120.0), typeLabel.height)];
        NSMutableAttributedString *explainAttr = [[NSMutableAttributedString alloc] initWithString:@"1-3个工作日内退款到支付方" attributes:@{NSFontAttributeName : KFontSize(16.0), NSForegroundColorAttributeName : kColorHEX(0x666666)}];
        [explainAttr addAttributes:@{NSForegroundColorAttributeName : kColorHEX(0xDD4B39)} range:NSMakeRange(0, 3)];
        [explainLabel setAttributedText:explainAttr];
        [returnTypeBgView addSubview:explainLabel];
        
    }
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(220.0))];
    footView.backgroundColor = kColorHEX(0xF0F0F0);
    UIView *infoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(8.0), self.width, KScaleWidth(140.0) - KScaleWidth(8.0))];
    infoBgView.backgroundColor = kColorHEX(0xffffff);
    [footView addSubview:infoBgView];
    footView.clipsToBounds = YES;
    infoBgView.clipsToBounds = YES;
    
    UILabel *explainTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(12.0), self.width, KScaleWidth(30.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
    explainTitleLabel.text = @"申请说明";
    [infoBgView addSubview:explainTitleLabel];
    [infoBgView addSubview:self.explainTextView];
    [_explainTextView addSubview:self.placeHolderLabel];
    [infoBgView addSubview:self.wordNumLabel];
    
    UIButton *applyForBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, infoBgView.maxY + KScaleWidth(32.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"申请退款" titleColor:kColorHEX(0x333333)];
    [applyForBtn addTarget:self action:@selector(applyForAction:) forControlEvents:UIControlEventTouchUpInside];
    [applyForBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [applyForBtn setCornerRadius:applyForBtn.height/2];
    [footView addSubview:applyForBtn];
    _tbl.tableHeaderView = headView;
    _tbl.tableFooterView = footView;
}

- (void)setContentWithMeetDetailModel:(XMOrderDetailModel *)orderDetailModel {
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:orderDetailModel.avatar] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    _nameLabel.text = orderDetailModel.nickname;
    [_meetInfoView setContentWithOrderDetailModel:orderDetailModel];
    _payPriceLabel.text = [NSString stringWithFormat:@"¥%@", orderDetailModel.yinfuPrice];
    _returnPriceLabel.text = [NSString stringWithFormat:@"¥%@", orderDetailModel.yinfuPrice];
}

- (void)setContentWithRuleArr:(NSString *)ruleStr {
    for (UIView *subView in self.ruleBgView.subviews) {
        [subView removeFromSuperview];
    }
    NSArray *ruleArr = [ruleStr componentsSeparatedByString:@"-"];
    for (int i = 0; i < ruleArr.count; i ++) {
        UILabel *ruleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(5.0) + KScaleWidth(24.0)*i, self.width - KScaleWidth(16.0)*2, KScaleWidth(24.0)) font:KFontSize(14.0) textColor:kColorHEX(0x999999)];
        ruleLabel.text = [NSString stringWithFormat:@"%d. %@", i + 1, ruleArr[i]];
        [self.ruleBgView addSubview:ruleLabel];
    }
    _ruleBgView.height = KScaleWidth(5) + ruleArr.count*KScaleWidth(24.0) + KScaleWidth(5.0);
    _ruleBgView.superview.height = _ruleBgView.maxY;
    _returnTypeBgView.y = _ruleBgView.superview.maxY + KScaleWidth(8.0);
    _returnTypeBgView.superview.height = _returnTypeBgView.maxY;
    _tbl.tableHeaderView = _returnTypeBgView.superview;
}

//申请
- (void)applyForAction:(UIButton *)button {
    NSString *reasonMy = @[@"计划有变", @"拍错不想去了", @"不能马上见面"][_myReasonSelectIndex];
    NSString *reasonOther = @[@"对方服务态度差", @"对方告知不能按时见面", @""][_otherReasionSelectIndex];
    self.applyForBlock ? self.applyForBlock(reasonMy, reasonOther, self.explainTextView.text) : nil;
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    else {
        return 2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return KScaleWidth(38.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(38.0))];
    headView.backgroundColor = kColorHEX(0xF6F6F6);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, self.width - KScaleWidth(16.0), KScaleWidth(38.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    [headView addSubview:titleLabel];
    titleLabel.text = @[@"个人原因", @"对方原因"][section];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(44.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMApplyForReturnReasonCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.textLabel.font = KFontSize(13.0);
        cell.textLabel.textColor = kColorHEX(0x333333);
        UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(self.width - KScaleWidth(50.0), 0, KScaleWidth(50.0), KScaleWidth(44.0)) image:@"mine_select_icon" selectImg:@"mine_select_icon_select"];
        selectBtn.userInteractionEnabled = NO;
//        [selectBtn addTarget:self action:@selector(reasonSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        selectBtn.tag = 32;
        [cell addSubview:selectBtn];
    }
    UIButton *selectBtn = [cell viewWithTag:32];
    if (indexPath.section == 0) {
        selectBtn.selected = _myReasonSelectIndex == indexPath.row;
    }
    else {
        selectBtn.selected = _otherReasionSelectIndex == indexPath.row;
    }
//    NSString *str = [NSString stringWithFormat:@"%ld-%ld", indexPath.section, indexPath.row];
//    [selectBtn setTitle:str forState:UIControlStateDisabled];
    if (indexPath.section == 0) {
        cell.textLabel.text = @[@"计划有变", @"拍错不想去了", @"不能马上见面"][indexPath.row];
    }
    else {
        cell.textLabel.text = @[@"对方服务态度差", @"对方告知不能按时见面"][indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        _myReasonSelectIndex = indexPath.row;
    }
    else {
        _otherReasionSelectIndex = indexPath.row;
    }
    [tableView reloadData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self endEditing:YES];
}

- (void)textViewDidChange:(UITextView *)textView {
    _placeHolderLabel.hidden = textView.text.length > 0;
    if (textView.text.length >= 150) {
        textView.text = [textView.text substringToIndex:150];
    }
    _wordNumLabel.text = [NSString stringWithFormat:@"%ld/150", textView.text.length];
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    [self endEditing:YES];
}

////原因选择
//- (void)reasonSelectAction:(UIButton *)button {
//    NSString *str = [button titleForState:UIControlStateDisabled];
//    NSString *sectionStr = [[str componentsSeparatedByString:@"-"] firstObject];
//    NSString *rowStr = [[str componentsSeparatedByString:@"-"] lastObject];
//    if ([sectionStr isEqualToString:@"0"]) {
//        _myReasonSelectIndex = [rowStr intValue];
//    }
//    else {
//        _otherReasionSelectIndex = [rowStr intValue];
//    }
//    [_tbl reloadData];
//}

#pragma mark - Init

- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tbl.separatorColor = kColorHEX(0xF1F1F1);
        _tbl.backgroundColor = kColorHEX(0xF0F0F0);
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(36.0), KScaleWidth(36.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(60.0), KScaleWidth(16.0), self.width - KScaleWidth(60.0), KScaleWidth(36.0)) font:KFontSize(18.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

- (XMConfirmOrderMeetCell *)meetInfoView {
    if (!_meetInfoView) {
        _meetInfoView = [[XMConfirmOrderMeetCell alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(100.0))];
    }
    return _meetInfoView;
}

- (UILabel *)payPriceLabel {
    if (!_payPriceLabel) {
        _payPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width/2, KScaleWidth(8.0), self.width/2 - KScaleWidth(16.0), KScaleWidth(30.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentRight];
    }
    return _payPriceLabel;
}

- (UILabel *)returnPriceLabel {
    if (!_returnPriceLabel) {
        _returnPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.payPriceLabel.x, _payPriceLabel.maxY, _payPriceLabel.width, _payPriceLabel.height) font:KFontSize(18.0) textColor:kColorHEX(0xDD4B39) textAlignment:NSTextAlignmentRight];
    }
    return _returnPriceLabel;
}

- (UIView *)ruleBgView {
    if (!_ruleBgView) {
        _ruleBgView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(105.0), self.width, KScaleWidth(100.0))];
//        _ruleBgView.backgroundColor = kColorHEX(0x00ffff);
    }
    return _ruleBgView;
}

- (UITextView *)explainTextView {
    if (!_explainTextView) {
        _explainTextView = [[UITextView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(45.0), self.width - KScaleWidth(16.0)*2, KScaleWidth(52.0))];
        _explainTextView.font = KFontSize(13.0);
        _explainTextView.delegate = self;
        _explainTextView.returnKeyType = UIReturnKeyDone;
    }
    return _explainTextView;
}

- (UILabel *)placeHolderLabel {
    if (!_placeHolderLabel) {
        _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width - KScaleWidth(16.0)*2, KScaleWidth(30.0)) font:KFontSize(13.0) textColor:kColorHEX(0x999999)];
        _placeHolderLabel.text = @"请填写申请说明，以便对方和平他更好的处理";
        _placeHolderLabel.userInteractionEnabled = NO;
    }
    return _placeHolderLabel;
}

- (UILabel *)wordNumLabel {
    if (!_wordNumLabel) {
        _wordNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, KScaleWidth(97.0), self.width - KScaleWidth(16.0), KScaleWidth(18.0)) font:KFontSize(13.0) textColor:kColorHEX(0x999999) textAlignment:NSTextAlignmentRight];
        _wordNumLabel.text = @"0/150";
    }
    return _wordNumLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
